<?php

namespace app\common\controller\zhongzhuan;

use app\admin\library\Auth;
use think\Config;
use think\Controller;
use think\Db;
use ec\Ecbase as Eccommon;

/**
 * 后台控制器基类
 */
class Autoassign extends Controller{
    
    
    /**
     * 
     * @param type $request_data
     * $list = [
            array(
                "followUserId" => ,
                "mobile" => 18300000015,
                "name" => "测试数据15",
                "fields"=>[
                    '82705932'=>'C级(明确拒绝)',
                    '82720068'=>'https://byrobot-prod.oss-cn-hangzhou.aliyuncs.com/20240807/20240807NbT05l8528034362_19553058820.mp3'
                ]
            )
        ];
     */
    public static function startAssign($request_data){
        
        $assign_id = empty($request_data['assign_id'])?0:$request_data['assign_id'];
        
        $kuhu_info =  empty($request_data['kehu_info'])?[]:$request_data['kehu_info'];
        
        $user_id =  empty($request_data['user_id'])?0:$request_data['user_id'];
        
        if(empty($assign_id) || empty($kuhu_info) || empty($user_id)) return false;
        
        $user_info = db('ec_assign_kehu')->alias('a')->field('a.id assign_id,a.count,a.daituisong_count,u.id ecid,b.id assign_detail_id, b.people,b.success_people,u.userid,u.name username,u.account')
                ->join('ec_assign_kehu_detail b','a.id=b.assign_id')   
                ->join('ec_user u','b.user_id = u.id')
                ->where('a.id',$assign_id)
//                ->where('b.yixiang_id',$kuhu_info['yixiang_id'])
                ->where('b.assign_status',1)
                ->where('u.is_shangban',1)
                ->where('b.user_id',$user_id)
                ->order('b.success_people asc,b.people desc')
                ->find();
       
        
        
  
        if(empty($user_info)) return false;
        $assign_data = self::assingSignleKehuToUser($kuhu_info,$user_info);
        
        return $assign_data;
        
        try{
            Db::startTrans();
            
            
            db('ec_aiuser_to_ecuser')->insertGetId($assign_data);
            
            db(getBaiYingDataTable())->where('id','in',$assign_data['byid'])->setField('status',1);
            
            $success_people = $user_info['success_people'] + 1;
            $save_data = ['success_people'=>$success_people];              
            if($success_people >= $user_info['people']){
                $save_data['assign_status'] = 2;
            }
            db('ec_assign_kehu_detail')->where('id',$user_info['assign_detail_id'])->update($save_data);
            
            $daituisong_count = $user_info['daituisong_count']  + 1;
            
            if($daituisong_count >= $user_info['count']){             
               db('ec_assign_kehu')->where('id',$assign_id)->update(['daituisong_count'=>$daituisong_count,'status'=>2]);
            }else{
                db('ec_assign_kehu')->where('id',$assign_id)->update(['daituisong_count'=>$daituisong_count,'status'=>1]);
            }
            
            Db::commit();
        }catch(\Exception $e){
            Db::rollback();
        }
        
    }
    
    public static function getAssingUserId($request_data){
        $list = $request_data['list'];
        $end_list = [];
        $min = 999999;
        $user_id = 0;
        foreach ($list as $key=> $value){
            if($value['success_people'] >= $value['people']) continue;
            if($value['success_people'] < $min){
                $min = $value['success_people'];
                $user_id = $key;
            }
        }
                       
        
        
        return ['user_id'=>$user_id,'list'=>$list] ;
    }

    





    public static function assingSignleKehuToUser($kehu_item,$user_info){
        $field_id_config = config('zhongzhuan.field');
        $yixiang_field_id = $field_id_config['yixiang']['fieldid'];
        $baiying_luyin_field_id = $field_id_config['baiying_luyin']['fieldid'];
        $baiying_luyin_duration_field_id = $field_id_config['baiying_luyin_duration']['fieldid'];
        
        $zhiying_channelId= config('zhongzhuan.zhiying_channelId');
        
        $luyin_domain_prefix = config('zhongzhuan.luyin_domain_prefix');
        
        
        $luyinOssUrl = $luyin_domain_prefix . '?callInstanceId='.$kehu_item['callInstanceId'];
        $duration = empty($kehu_item['duration']) ? 0 : $kehu_item['duration'];
        
        
        $kehu_item_name = $kehu_item['customerName'];
        $kehu_item_name = empty($kehu_item_name) ? '未命名' .substr($kehu_item['customerTelephone'], -4) : $kehu_item_name;
       
        $temp_data = [
            "followUserId"=>$user_info['userid'],"mobile"=>$kehu_item['customerTelephone'],"name"=>$kehu_item_name,"channelId"=>$zhiying_channelId,
            "fields"=>[$yixiang_field_id=>$kehu_item['yixiang_txt'],$baiying_luyin_field_id=>$luyinOssUrl,$baiying_luyin_duration_field_id=>$duration]
        ];
        
        $startTime = strlen($kehu_item['startTime']) > 10 ?  $kehu_item['startTime'] / 1000 : $kehu_item['startTime'];
        
        $middle_data = [
            "callJobId"=>$kehu_item['callJobId'],"mobile"=>$kehu_item['customerTelephone'],"name"=>$kehu_item_name,"createtime"=>time(),'yixiang_value'=>$kehu_item['yixiang_value'],
            'assign_id'=>$user_info['assign_id'],'ecid'=>$user_info['ecid'],'byid'=>$kehu_item['byid'],'yixiang_txt'=>$kehu_item['yixiang_txt'],"starttime"=>date('Y-m-d H:i:s',$startTime),
            "content"=> json_encode($temp_data,JSON_UNESCAPED_UNICODE)
        ];
       
        

        
        return $middle_data;
        
    }
}
